package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.JpushForMessageUtil;
import com.china08.yxyapi.util.JpushForNotificationUtil;

/**
 * 教育局行政办公 外部通知 新增通知
 * 
 */
@CrossOrigin
@RestController("EduOuterNoticeCrtController")
public class OuterNoticeCrtController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        RespModel respModel = new RespModel();
        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String userNick = yxyProfile.getUserNick();
        String avatar = yxyProfile.getUserAvator();
        String edbName = yxyProfile.getSchoolName();

        // 接受人id
        List<String> receiveSchoolIdLst = reqModel.receiverSchoolId;
        List<String> receiverSchoolNameLst = reqModel.receiverSchoolName;
        List<YxyNotifictionSchool> receiverSchool = new ArrayList<YxyNotifictionSchool>();
        YxyNotifictionSchool yxyNotifictionSchool = null;
        for (int i = 0; i < receiveSchoolIdLst.size(); i++) {
            yxyNotifictionSchool = new YxyNoticeOutter().new YxyNotifictionSchool();
            yxyNotifictionSchool.setSchoolId(receiveSchoolIdLst.get(i));
            yxyNotifictionSchool.setSchoolName(receiverSchoolNameLst.get(i));
            yxyNotifictionSchool.setIsDelete(0);
            yxyNotifictionSchool.setIsRead(0);
            receiverSchool.add(yxyNotifictionSchool);
        }
        // FIXED 业务库
        YxyNoticeOutter yxyNoticeOutter = new YxyNoticeOutter();
        yxyNoticeOutter.setUserId(currentUserId);
        yxyNoticeOutter.setEdbId(schoolId);
        yxyNoticeOutter.setTitle(reqModel.title);
        yxyNoticeOutter.setContent(reqModel.content);
        yxyNoticeOutter.setReceiveSchool(receiverSchool);
        yxyNoticeOutter.setAttachmentName(reqModel.attachmentName);
        yxyNoticeOutter.setAttachmentPath(reqModel.attachmentPath);
        yxyNoticeOutter.setDeleteStatus(0);
        yxyNoticeOutter.setImage(null);
        yxyNoticeOutter.setUserNick(userNick);
        yxyNoticeOutter.setAvatar(avatar);
        yxyNoticeOutter.setEdbName(edbName);
        yxyNoticeOutterRepository.save(yxyNoticeOutter);

        // 发送消息
        List<Integer> roleIdList = new ArrayList<>();
        roleIdList.add(2);

        for (String string : receiveSchoolIdLst) {
            List<String> list = new ArrayList<>();
            list.add(string);
            JpushForMessageUtil.sendMessage(3, roleIdList, string, list, null);

            // 发送通知
            Map<String, String> map = new HashMap<String, String>();
            map.put("modelId", "educationNotice");
            List<String> tabAndList = new ArrayList<>();
            tabAndList.add("SCH" + string);
            JpushForNotificationUtil.sendNotification(reqModel.title, "教育局通知", map, list, tabAndList);
        }

        respModel.setMessage("发通知成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 标题
        public String title;
        // 内容
        public String content;
        // 接受对象列表id
        public List<String> receiverSchoolId;
        // 接受对象列表name
        public List<String> receiverSchoolName;
        // 附件原始文件名称
        public String attachmentName;
        // 附件地址
        public String attachmentPath;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
